

import socket
import time

def get_local_public_ip(public_ip_server='ddns.cs-iot.cn'):

    BUFSIZE = 1024
    client = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
    client.settimeout(5)
    hostip =socket.gethostbyname(public_ip_server)
    #hostip =socket.gethostbyname(public_ip_server)
    print(hostip)

    msg = "012"
    ip_port = (hostip, 9999)
    client.sendto(msg.encode('utf-8'),ip_port)
    data,server_addr = client.recvfrom(BUFSIZE)
    print('客%s--%s'%(data,server_addr))
    client.close()

    public_ip_value = data
    return public_ip_value

if __name__ == "__main__":
    my_public_ip = get_local_public_ip("ddns.cs-iot.cn")
    print(my_public_ip)